<?php
/**
 * Greendizer PHP Library
 * 
 * @category    Greendizer
 * @package     Greendizer_Containers
 * @subpackage  Greendizer_Containers_Buyers
 * @copyright   Copyright (c) 2009-2010, Greendizer S.A - All rights reserved.
 * @license     Greendizer/Licence.txt
 * @version     1.0
 */

/**
 * @see Greendizer_Containers_MessageContainerBase
 */
require_once 'Greendizer/Containers/MessageContainerBase.php';

/**
 * @see Greendizer_Collections_Buyers_MessageCollection
 */
require_once 'Greendizer/Collections/Buyers/MessageCollection.php';

/**
 * @see Greendizer_Resources_Buyers_Message
 */
require_once 'Greendizer/Resources/Buyers/Message.php';

/**
 * Represents an message container for buyers in Greendizer
 * 
 * @category    Greendizer
 * @package     Greendizer_Containers
 * @subpackage  Greendizer_Containers_Buyers
 * @copyright   Copyright (c) 2009-2010, Greendizer S.A - All rights reserved.
 * @license     Greendizer/License.txt
 */
class Greendizer_Containers_Buyers_MessageContainer extends Greendizer_Containers_MessageContainerBase{
    
    /**
     * Returns the collection of messages for a search query 
     *
     * @param string $queryString   Query of the search
     * @return Greendizer_Collections_Buyers_MessageCollection 
     */
    public function search($queryString) {
        parse_str($queryString, $query);
        return new Greendizer_Collections_Buyers_MessageCollection($this, new Greendizer_Net_Uri($this->getBaseUri()->getAbsoluteUri(), $queryString));
    }
    
    /**
     * Retrieves a message given its ID
     *
     * @param string $id    message ID
     * @return Greendizer_Resources_Buyers_Message 
     */
    public function getById($id) {
        $threadUri = new Greendizer_Net_Uri(substr($this->getBaseUri()->getAbsoluteUri(), 0, (-1) * strlen('messages/')));
        return new Greendizer_Resources_Buyers_Message($this->getClient(), $threadUri, $id);
    }
    
    /**
     * Returns a collection containing all the messages available
     *
     * @return Greendizer_Collections_Buyers_InvoiceCollection 
     */
    public function getAll(){
        return new Greendizer_Collections_Buyers_InvoiceCollection($this, new Greendizer_Net_Uri($this->getBaseUri()->getAbsoluteUri()));
    }
    
    /**
     * Sends a new message and returns its object
     *
     * @param string $text  Message text
     * @return Greendizer_Resources_Buyers_Message 
     */
    public function send($text){
        return parent::send($text);
    }
    
    /**
     * Creates and returns a message object
     *
     * @param array $resourceProperties Key/Value pairs of property names and values
     * @return Greendizer_Resources_Buyers_Message
     */
    protected function create($resourceProperties) {
        //Send data on the server
        $response = parent::create($resourceProperties);

        //Build and return the desired object
        $threadUri = new Greendizer_Net_Uri(substr($this->getBaseUri()->getAbsoluteUri(), 0, (-1) * strlen('messages/')));
        $resource = new Greendizer_Resources_Buyers_Message(
                        $this->client,
                        $threadUri,
                        Greendizer_Net_Etag::parseEtagString($response->getHeader('etag'))->getId()
        );
        $resource->sync(json_decode($response->getData()), Greendizer_Net_Etag::parseEtagString($response->getHeader('etag')));
        return $resource;
    }
    
    
}

?>
